﻿using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;

namespace XJIE.Business.Common.Helper.Mail
{


    public class SmtpHelper
    {
        /// <summary>
        /// Smtp客户端连接
        /// </summary>
        private static SmtpClient _Smtpclient;
        /// <summary>
        /// 发件人姓名
        /// </summary>
        private static string _SenderName;

        /// <summary>
        /// 构造
        /// </summary>
        public SmtpHelper()
        {

        }

        /// <summary>
        /// 构造创建Smtp客户端
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="smtpHost"></param>
        /// <param name="smtpPort"></param>
        public SmtpHelper(string username, string password, string smtpHost, int smtpPort, string senderName = "easybom")
        {
            //构建Smtp连接
            _Smtpclient = new SmtpClient(smtpHost, smtpPort);
            _Smtpclient.EnableSsl = true;
            _Smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
            _Smtpclient.Credentials = new NetworkCredential(username, password);
            _SenderName = senderName;
        }

        /// <summary>
        /// 使用新Smtp连接
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="smtpHost"></param>
        /// <param name="smtpPort"></param>
        /// <param name="enableSSL"></param>
        /// <param name="smtpDeliveryMethod"></param>
        public SmtpClient UseNewConnection(string username, string password, string smtpHost, int smtpPort, bool enableSSL, SmtpDeliveryMethod smtpDeliveryMethod)
        {
            var client = new SmtpClient(smtpHost, smtpPort);
            client.EnableSsl = enableSSL;
            client.DeliveryMethod = smtpDeliveryMethod;
            client.Credentials = new NetworkCredential(username, password);
            return client;
        }

        /// <summary>
        /// Smtp发送邮件-支持群发,群发用;隔开
        /// </summary>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="isHtml"></param>
        /// <param name="Attachment">附件</param>
        /// <param name="client">可自定义smtp客户端,不传则使用默认客户端</param>
        public void Send(string to, string subject, string body, bool isHtml, Attachment Attachment = null, SmtpClient client = null)
        {
            MailMessage message = new MailMessage();
            string[] ts = to.Split(';');
            foreach (var item in ts)
            {
                message.To.Add(item.Trim());
            }
            message.IsBodyHtml = isHtml;
            message.Subject = subject;
            message.Body = body;
            if (Attachment != null)
            {
                message.Attachments.Add(Attachment);
            }

            if (client == null)
            {
                message.From = new MailAddress(_Smtpclient.Credentials.GetCredential(_Smtpclient.Host, _Smtpclient.Port, null).UserName, _SenderName);
                //发送邮件
                _Smtpclient.Send(message);
            }
            else
            {
                message.From = new MailAddress(client.Credentials.GetCredential(client.Host, client.Port, null).UserName, _SenderName);
                //发送邮件
                client.Send(message);
            }
        }

        /// <summary>
        /// 异步Smtp发送邮件-支持群发,群发用;隔开
        /// </summary>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="isHtml"></param>
        /// <param name="Attachment">附件</param>
        /// <param name="client">可自定义smtp客户端,不传则使用默认客户端</param>
        public async Task SendAsync(string to, string subject, string body, bool isHtml, Attachment Attachment = null, SmtpClient client = null)
        {
            MailMessage message = new MailMessage();
            string[] ts = to.Split(';');
            foreach (var item in ts)
            {
                message.To.Add(item.Trim());
            }
            message.IsBodyHtml = isHtml;
            message.Subject = subject;
            message.Body = body;
            if (Attachment != null)
            {
                message.Attachments.Add(Attachment);
            }

            if (client == null)
            {
                message.From = new MailAddress(_Smtpclient.Credentials.GetCredential(_Smtpclient.Host, _Smtpclient.Port, null).UserName, _SenderName);
                //发送邮件
                await _Smtpclient.SendMailAsync(message);
            }
            else
            {
                message.From = new MailAddress(client.Credentials.GetCredential(client.Host, client.Port, null).UserName, _SenderName);
                //发送邮件
                await client.SendMailAsync(message);
            }
        }

    }
}
